package com.twu.biblioteca;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Anbu
 * Date: 10/2/13
 * Time: 11:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class XMLDataReader implements DataReader {

    private final Document document;

    public XMLDataReader(Document document) throws ParserConfigurationException, IOException, SAXException {
        this.document = document;
    }

    public static XMLDataReader buildXMLDataReader(String fileName) throws ParserConfigurationException, SAXException, IOException {
        File file = new File(fileName);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        return new XMLDataReader(document);
    }

    @Override
    public void loadBookData(Library library) {
        NodeList bookList = document.getElementsByTagName("books").item(0).getChildNodes();
        for(int bookIterator = 0;bookIterator < bookList.getLength(); bookIterator++){
            Node bookNode = bookList.item(bookIterator);
            if(bookNode.getNodeType() == Node.ELEMENT_NODE){
                Element bookElement = (Element) bookNode;
                Book book = Book.buildBook(bookElement.getAttribute("isbn"), bookElement.getAttribute("name"), bookElement.getAttribute("author"));
                loadCopies(bookNode, book);
                library.addBook(book);
            }

        }
    }

    @Override
    public void loadMovieData(Library library) {
        NodeList movieList = document.getElementsByTagName("movies").item(0).getChildNodes();
        for(int movieIterator = 0;movieIterator < movieList.getLength(); movieIterator++){
            Node movieNode = movieList.item(movieIterator);
            if(movieNode.getNodeType() == Node.ELEMENT_NODE){
                Element element = (Element) movieNode;
                Movie movie = Movie.buildMovie(element.getAttribute("id"),
                        element.getAttribute("name"),
                        Integer.parseInt(element.getAttribute("year")),
                        element.getAttribute("director"), Double.parseDouble(element.getAttribute("rating")));
                loadCopies(movieNode, movie);
                library.addMovie(movie);
            }

        }
    }

    private void loadCopies(Node node, ReservableItem reservableItem) {
        NodeList copyList = node.getChildNodes();
        for(int copyIterator = 0; copyIterator < copyList.getLength(); copyIterator++){
            Node copyNode = copyList.item(copyIterator);
            if(copyNode.getNodeType() == Node.ELEMENT_NODE){
                reservableItem.addCopy(((Element) copyNode).getAttribute("id"));
            }
        }
    }
}
